﻿//======================================================================
//  用户界面相关
//====================================================================== 

using System;
using System.Collections.Generic;

using System.Text;
using System.Web;

namespace Common
{
    /// <summary>
    /// 用户界面相关
    /// </summary>
    public class UIHelp
    {
        #region 弹出消息对话框

        /// <summary>
        /// 弹出消息对话框(不推荐使用,请使用重载其他方法,避免弹框后页面CSS改变)
        /// </summary>
        /// <param name="message">消息字符串</param>
        /// <param name="end">是否结束本页</param>
        /// <param name="url">重定向URL</param>
        public static void AlertMessage(string message, bool end, string url)
        {
            HttpContext.Current.Response.Write("<script type=\"text/javascript\">");
            HttpContext.Current.Response.Write("alert(\"" + message + "\");");


            if (url != null)
            {
                HttpContext.Current.Response.Write("document.location.href=\"" + url + "\";");
            }
            HttpContext.Current.Response.Write("</script>");

            if (end)
                HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 弹出消息对话框(不推荐使用,请使用重载其他方法,避免弹框后页面CSS改变)
        /// </summary>
        /// <param name="message">消息字符串</param>
        /// <param name="url">重定向URL</param>
        public static void AlertMessage(string message, string url)
        {
            AlertMessage(message, true, url);
        }

        /// <summary>
        /// 弹出消息对话框(不推荐使用,请使用重载其他方法,避免弹框后页面CSS改变)
        /// </summary>
        /// <param name="message">消息字符串</param>
        /// <param name="end">是否结束本页</param>
        public static void AlertMessage(string message, bool end)
        {
            AlertMessage(message, end, null);
        }
        /// <summary>
        /// 弹出消息对话框(不推荐使用,请使用重载其他方法,避免弹框后页面CSS改变)
        /// </summary>
        /// <param name="message">消息字符串</param>
        public static void AlertMessage(string message)
        {
            AlertMessage(message, false, null);
        }

        /// <summary>
        /// 页面加载完毕后,弹出消息对话框
        /// </summary>
        /// <param name="page">需要弹框的页面</param>
        /// <param name="message">消息字符串</param>
        public static void AlertMessage(System.Web.UI.Page page, string message)
        {
            ExecScriptAfterLoad(page,"alert('" + message + "')");
        }

        #endregion

        #region 执行JS

        /// <summary>
        /// 立即执行Script脚本
        /// </summary>
        /// <param name="page">当前web页面</param>
        /// <param name="script">执行的script脚本</param>
        public static void ExecScript(System.Web.UI.Page page, string script)
        {
            if ((page == null) || (script == null))
                return;
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), Guid.NewGuid().ToString(), "<script type=\"text/javascript\">" + script + "</script>");
        }

        /// <summary>
        /// 页面加载完毕后执行脚本
        /// </summary>
        /// <param name="page"></param>
        /// <param name="script"></param>
        public static void ExecScriptAfterLoad(System.Web.UI.Page page, string script)
        {
            if ((page == null) || (script == null))
                return;
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), "<script type=\"text/javascript\">" + script + "</script>");
        }

        #endregion

        #region 跳转到错误页面

        /// <summary>
        /// 跳转到错误页面
        /// </summary>
        /// <param name="code">错误编号</param>
        /// <param name="errorMessage">显示给客户看的友好中文提示信息</param>
        /// <param name="errorDetail">网站管理员看到的详细的利于查找错误的信息</param>
        public static void ToErrorPage(string code, string errorMessage, string errorDetail)
        {
            string url = "Error.aspx";
            url += "?msg=" + errorMessage;
            url += "&detail=" + errorDetail;
            url += "&code=" + code;
            HttpContext.Current.Response.Redirect(url);
        }

        #endregion
    }
}
